let util = {};
util.request = option => {
  let app = getApp();
  let i = app.siteInfo.uniacid;
  let siteroot = app.siteInfo.siteroot;
  let url = `${siteroot}?i=${i}&from=wxapp&c=visualization&a=api`;
  return new Promise((resolve, reject) => {
    wx.request({
      url,
      data: option.data ? option.data : {},
      header: option.header ? option.header : {},
      method: option.method ? option.method : 'GET',
      header: {
        'content-type': 'application/json',
      },
      success: res => {
        resolve(res);
      },
      fail: res => {
        reject(res);
      },
    });
  });
};

util.getComsCollectionAndComsOrder = () => {
  // 获取所有功能模块
  let app = getApp();
  let m = app.siteInfo.module;
  return util.request({
    data: { type: 'block', m },
  }).then(res => {
    return res.data;
  });
};

// 根据type值获取自定义组件和风格颜色值
/**
 * type: customComponent 自定义组件
 *       getColor 风格颜色值
 */
util.getCustomDataByType = (getType) => {
  let app = getApp();
  let m = app.siteInfo.module;
  const data = {
    type: getType,
    m,
  };
  const shopid = app.ws_micro_shop_globalData && app.ws_micro_shop_globalData.shopid;
  if (shopid) {
    data.shop_id = shopid;
  }
  return util.request({ data }).then(res => {
    return res.data;
  });
};

util.getComContent = options => {
  // 获取功能模块的内容数据
  let type = options.type;
  let uComID = options.comID;
  let genneryID = options.genneryID || 0;
  return util.request({
    data: { type, uComID, genneryID },
  }).then(res => {
    return res.data;
  });
};

util.getComConfig = options => {
  // 获取功能模块的配置数据
  let type = 'post';
  let comID = options.comID;
  let genneryID = options.genneryID || 0;
  return util.request({
    data: { type, comID, genneryID },
  }).then(res => {
    return res.data;
  });
};

util.getCustomPageTitle = () => {
  let app = getApp();
  let m = app.siteInfo.module;
  const data = {
    m,
    type: 'getHeadline',
  };
  const shopid = app.ws_micro_shop_globalData && app.ws_micro_shop_globalData.shopid;
  if (shopid) {
    data.shop_id = shopid;
  }
  return util.request({ data }).then(res => {
    return res.data;
  });
};

module.exports = util;
